﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;
using Google.Api.Maps.Service.StaticMaps;

namespace NextViewer
{
    class Http
    {
        public Http(NextViewer next)
        {
            nv = next;
        }
        public Image UpdateMap(StaticMap sm)
        {
            Image image;
            //https://maps.google.com/maps/api/staticmap?center=51.477222,0&zoom=14&size=640x640&sensor=false

            // Create a request for the URL. 
            WebRequest request = WebRequest.Create(sm.ToUri());
            request.Timeout = 50000;
            string debug = sm.ToUri().ToString();
              //"https://maps.google.com/maps/api/staticmap?center=51.477222,0&zoom=14&size=640x640&sensor=false");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            try
            {
                WebResponse response = request.GetResponse();
                // Display the status.
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                //MemoryStream ms = new MemoryStream(dataStream);
                image = Image.FromStream(dataStream);
            }
            catch (Exception ee)
            {
                nv.SetText(ee.ToString());
                image = null;
            }
            return image;
        }
        NextViewer nv;
    }
}
